
cc.Class({
    extends: cc.Component,

    properties: {
        ranking: cc.Label,
        avatar: cc.Sprite,
        nickName: cc.Label,
        data: cc.Label,

        //地域
        location: cc.Node,
        city: cc.Label,
    },

    /**
     * data属性为： 
     * 
     * ranking
     * avatarUrl
     * city
     * nickName
     * data
     */
    init(data){
        if(!data){
            return;
        }
        //排名
        this.rank = data.ranking;
        this.ranking.string = data.ranking;
        //根据头像url加载玩家头像
        this.loadAvatar(data.avatarUrl, this.avatar);
        //位置
        if(data.city){
            this.location.active = true;
            this.city.string = data.city;
            this.nickName.node.y = -16;
        }else{
            this.location.active = false;
            this.nickName.node.y = 0;
        }
        //昵称
        this.nickName.string = data.nickName;//昵称省略...处理
        //关卡数
        this.data.string = "第" + data.data + "关";
    },

    getRank() {
        return this.rank;
    },

    loadAvatar(url, sprite){
        try{
            cc.loader.load({url: url, type: 'png'}, function (err, texture) {
                if(err){
                    console.error(err.message || err);
                }
                sprite.spriteFrame = new cc.SpriteFrame(texture);
            });
        }catch(e){
            console.error(e);
            sprite.node.active = false;
        }
    },

    
});
